using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Data.SqlClient;

namespace DataAccessLayer
{
    public class DAO_Question_Item
    {
        public static bool Insert(Object tmp)
        {
            QUESTION_ITEM obj = (QUESTION_ITEM)tmp;
            bool result = false;
            List<SqlParameter> _list = new List<SqlParameter>();
            _list.Add(new SqlParameter("@ITEM_FACE", obj.ITEM_FACE));
            _list.Add(new SqlParameter("@ITEM_VALUE", obj.ITEM_VALUE));
            _list.Add(new SqlParameter("@QUESTION_ID", obj.QUESTION_ID));

            int n = clsDataAccessHelper.ExecNonQuery("sp_question_item_insert", _list);
            if (n == 1)
                result = true;
            return result;
        }

        public static List<QUESTION_ITEM> Select(int questionID)
        {
            List<SqlParameter> _list = new List<SqlParameter>();
            _list.Add(new SqlParameter("@QUESTION_ID", questionID));

            DataTable table = clsDataAccessHelper.ExecuteQuery("sp_question_item_select", _list);
            if (table != null)
            {
                List<QUESTION_ITEM> data = new List<QUESTION_ITEM>();
                foreach (DataRow item in table.Rows)
                {
                    QUESTION_ITEM question_item = new QUESTION_ITEM();
                    question_item.ITEM_FACE = item["ITEM_FACE"].ToString();
                    question_item.ITEM_ID = decimal.Parse(item["ITEM_ID"].ToString());
                    question_item.ITEM_VALUE = item["ITEM_VALUE"].ToString();
                    question_item.QUESTION_ID = decimal.Parse(item["QUESTION_ID"].ToString());

                    data.Add(question_item);
                }
                return data;
            }
            return null;
        }

        public static bool Update(Object tmp)
        {
            QUESTION_ITEM obj = (QUESTION_ITEM)tmp;
            bool result = false;
            List<SqlParameter> _list = new List<SqlParameter>();
            _list.Add(new SqlParameter("@ITEM_FACE", obj.ITEM_FACE));
            _list.Add(new SqlParameter("@ITEM_ID", obj.ITEM_ID));
            _list.Add(new SqlParameter("@ITEM_VALUE", obj.ITEM_VALUE));
            _list.Add(new SqlParameter("@QUESTION_ID", obj.QUESTION_ID));

            int n = clsDataAccessHelper.ExecNonQuery("sp_question_item_update", _list);
            if (n == 1)
                result = true;
            return result;
        }

        public static string CurrentInsertID()
        {
            List<QUESTION> _list = new List<QUESTION>();
            String sql = "Select * From dbo.QUESTION_ITEM";
            DataTable table = clsDataAccessHelper.ExecuteQueryNonStoredProcedure(sql, null);
            if (table != null && table.Rows.Count > 0)
            {
                int max = 0;
                foreach (DataRow row in table.Rows)
                {
                    object item = row["ITEM_ID"];
                    if (item != null)
                    {
                        int _tmp = -1;
                        if (int.TryParse(item.ToString(), out _tmp))
                        {
                            if (_tmp > max) max = _tmp;
                        }
                    }
                }
                return max.ToString();
            }
            return "";
        }

        public static bool Delete(Object tmp)
        {
            QUESTION_ITEM obj = (QUESTION_ITEM)tmp;
            bool result = false;
            List<SqlParameter> _list = new List<SqlParameter>();
            //_list.Add(new SqlParameter("@ITEM_FACE", obj.ITEM_FACE));
            _list.Add(new SqlParameter("@ITEM_ID", obj.ITEM_ID));
            //_list.Add(new SqlParameter("@ITEM_VALUE", obj.ITEM_VALUE));
            _list.Add(new SqlParameter("@QUESTION_ID", obj.QUESTION_ID));

            int n = clsDataAccessHelper.ExecNonQuery("sp_question_item_delete", _list);
            if (n == 1)
                result = true;
            return result;
        }

        public static List<QUESTION_ITEM> LoadAll()
        {
            List<QUESTION_ITEM> _list = new List<QUESTION_ITEM>();
            
          
            return _list;
        }
    }
}
